function main( ui ) {

  var json = ui.getQueryParameters().getFirst("json");
  if ( json == null ){
        var ex = "{ exception: { message: 'no param provided'} }";
        return ex;
  }
  var jsonObject = new JSONObject( json );
  
  return getAppointments( jsonObject );

}


function getAppointments(jsonObject) {

  var records = new JSONArray();

  var cmd = "select t from EventEntity t where t.userId=:userId";
  
  
    EntityManager.find(  
    cmd,  
    jsonObject,
     function(result) {
      
      for( var i = 0; i < result.size() ; ++i ) {
        
        records.put( JSONObject.wrap(result.get(i)) );
      }
      
    });
    
    var result = new JSONObject();
    
    result.put( "records", records );
    

    return result.toString() ;

}
